package com.jay.android.fragment;

import com.jay.android.DashBoardActivity;
import com.jay.android.R;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.Toast;

@SuppressLint({ "NewApi", "ShowToast" })
public class NavigationFragment extends Fragment implements
		View.OnTouchListener, View.OnClickListener {
	private ImageButton dashBoardButton;
	private ImageButton clearErrorButton;
	private ImageButton mapButton;
	private ImageButton batteryButton;
	private ImageButton analysisButton;
	private ImageButton toolButton;
	// private TextView dashBoardTextView;
	// private TextView clearErrorTextView;
	// private TextView mapTextView;
	// private TextView batteryTextView;
	// private TextView analysisTextView;
	// private TextView toolTextView;

	private Toast mToast;
	private Context mContext;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		// return super.onCreateView(inflater, container, savedInstanceState);
		View rootView = inflater.inflate(R.layout.fragment_main_navi,
				container, false);

		resourceInit(rootView);

		mContext = getActivity();
		mToast = Toast.makeText(mContext, "", Toast.LENGTH_SHORT);
		return rootView;
	}

	private void showTip(final String str) {
		getActivity().runOnUiThread(new Runnable() {
			@Override
			public void run() {
				mToast.setText(str);
				mToast.show();
			}
		});
	}

	private void resourceInit(View rootView) {
		rootView.findViewById(R.id.line1).getBackground().setAlpha(50);
		rootView.findViewById(R.id.line2).getBackground().setAlpha(50);
		rootView.findViewById(R.id.line3).getBackground().setAlpha(50);
		rootView.findViewById(R.id.line4).getBackground().setAlpha(50);
		rootView.findViewById(R.id.line5).getBackground().setAlpha(50);

		dashBoardButton = (ImageButton) rootView
				.findViewById(R.id.btn_dash_board);
		clearErrorButton = (ImageButton) rootView
				.findViewById(R.id.btn_clear_error);
		mapButton = (ImageButton) rootView.findViewById(R.id.btn_map);
		batteryButton = (ImageButton) rootView.findViewById(R.id.btn_battery);
		analysisButton = (ImageButton) rootView.findViewById(R.id.btn_analysis);
		toolButton = (ImageButton) rootView.findViewById(R.id.btn_tool);
		dashBoardButton.setOnTouchListener(this);
		clearErrorButton.setOnTouchListener(this);
		mapButton.setOnTouchListener(this);
		batteryButton.setOnTouchListener(this);
		analysisButton.setOnTouchListener(this);
		toolButton.setOnTouchListener(this);
		dashBoardButton.setOnClickListener(this);
		clearErrorButton.setOnClickListener(this);
		mapButton.setOnClickListener(this);
		batteryButton.setOnClickListener(this);
		analysisButton.setOnClickListener(this);
		toolButton.setOnClickListener(this);
	}

	@Override
	public boolean onTouch(View view, MotionEvent event) {
		// TODO Auto-generated method stub
		switch (event.getActionMasked()) {
		case MotionEvent.ACTION_DOWN:
			((ImageButton) view).getDrawable().setAlpha(50);
			break;
		case MotionEvent.ACTION_UP:
			((ImageButton) view).getDrawable().setAlpha(255);
			break;
		case MotionEvent.ACTION_CANCEL:
			((ImageButton) view).getDrawable().setAlpha(255);
			break;
		default:
			break;
		}
		return false;
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.btn_dash_board:
			Intent intent = new Intent(getActivity(), DashBoardActivity.class);
			startActivity(intent);
			break;
		case R.id.btn_clear_error:
			showTip("清楚故障");

			break;
		case R.id.btn_map:
			showTip("地图导航");

			break;
		case R.id.btn_battery:
			showTip("电瓶管理");

			break;
		case R.id.btn_analysis:
			showTip("数据分析");

			break;
		case R.id.btn_tool:
			showTip("工具");

			break;

		default:
			break;
		}
	}
}
